package com.squash;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;


public class Digit extends View {

    private float mWidth;
    private float mHeight;
    private String mDigitString;
//    private Paint mDigitPaint;
//
//    private float mDigitX;
//    private float mDigitY;
    private int mCurrentDigit;

    private Paint mTextPaint = new TextPaint();
    private int mTextBaseline;


    public Digit(Context context) {
        super(context);
        initialize();
    }

    public Digit(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public Digit(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    public void setScore(int score)
    {
        mCurrentDigit = score;
        mDigitString = String.valueOf(mCurrentDigit);
        onSizeChanged(getWidth(), getHeight(), getWidth(), getHeight());
    }

    private void initialize()
    {

//        mDigitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        mDigitPaint.setColor(Color.WHITE);
//        mDigitPaint.setTextAlign(Paint.Align.CENTER);

        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        // draw the text
        // position is centered on width
        // and the baseline is calculated to be positioned from the
        // view bottom
        canvas.drawText(mDigitString, mWidth/2, mHeight-mTextBaseline, mTextPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;

//        mDigitPaint.setTextSize(h);
//
//        Rect bounds = new Rect();
//        mDigitPaint.getTextBounds(mDigitString, 0, mDigitString.length(), bounds);
//
//        int textHeight = Math.abs(bounds.height());
//
//        mDigitX = mWidth / 2;
//        mDigitY = mHeight - ((mHeight - textHeight) / 2);

        // first determine font point size
        adjustTextSize();
        // then determine width scaling
        // this is done in two steps in case the
        // point size change affects the width boundary
        adjustTextScale();

    }

    void adjustTextSize() {

        mTextPaint.setTextSize(100);
        mTextPaint.setTextScaleX(1.0f);
        Rect bounds = new Rect();
        // ask the paint for the bounding rect if it were to draw this
        // text
        mTextPaint.getTextBounds(mDigitString, 0, mDigitString.length(), bounds);

        // get the height that would have been produced
        int h = bounds.bottom - bounds.top;

        // make the text text up 70% of the height
        float target = (float)mHeight*.7f;

        // figure out what textSize setting would create that height
        // of text
        float size  = ((target/h)*100f);

        // and set it into the paint
        mTextPaint.setTextSize(size);
    }

    void adjustTextScale() {
        // do calculation with scale of 1.0 (no scale)
        mTextPaint.setTextScaleX(1.0f);
        Rect bounds = new Rect();
        // ask the paint for the bounding rect if it were to draw this
        // text.
        mTextPaint.getTextBounds(mDigitString, 0, mDigitString.length(), bounds);

        // determine the width
        int w = bounds.right - bounds.left;

        // calculate the baseline to use so that the
        // entire text is visible including the descenders
        int text_h = bounds.bottom-bounds.top;
        mTextBaseline=(int)(bounds.bottom+((mHeight-text_h)/2));

        // determine how much to scale the width to fit the view
        float xscale = ((float) (mWidth-getPaddingLeft()-getPaddingRight())) / w;

        // set the scale for the text paint
        mTextPaint.setTextScaleX(xscale);
    }

//    private void setDigitYValues() {
//
//        mDigitY = findCenterY(mCurrentDigit);
//    }
//
//    private float findCenterY(int digit)
//    {
//        String text = String.valueOf(digit);
//        Rect bounds = new Rect();
//        mDigitPaint.getTextBounds(text, 0, text.length(), bounds);
//
//        int textHeight = Math.abs(bounds.height());
//
//        float result = mHeight - ((mHeight - textHeight) / 2);
//
//        return result;
//    }



}
